package com.spit.tph.nike_mvp.ui

import CONSUMABLES_RECEIVE
import CONSUMABLES_RESTOCK
import CONSUMABLES_WAREHOUSE
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.google.zxing.integration.android.IntentIntegrator
import com.spit.tph.Event.nike_event.DialogCallbackEvent
import com.spit.tph.MainActivity
import com.spit.tph.R
import com.spit.tph.SystemFragment.BaseFragment
import com.spit.tph.nike_mvp.adapter.ConsumablesDescAdapter
import com.spit.tph.nike_mvp.impl.ConsumablesContract
import com.spit.tph.nike_mvp.presenter.ConsumablesPresenter
import com.spit.tph.nike_mvp.utils.ToastUtil
import com.spit.tph.nike_mvp.weight.ConsumablesConfigFrg
import com.yyc.stocktake.bean.DataBean
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode


/**
 * User: Nike
 *  2024/2/27 16:23
 */
class ConsumablesDescFrg: BaseFragment(), View.OnClickListener, ConsumablesContract.View, MainActivity.OnActivityResultListener1{

    val mPresenter by lazy { ConsumablesPresenter() }

    var listBean = ArrayList<DataBean>()
    //团队列表
    var listTeamBean = ArrayList<DataBean>()
    //仓库列表
    var listWarehouseBean = ArrayList<DataBean>()
    //物资列表
    var listMaterialBean = ArrayList<DataBean>()
    //治疗师列表
    var listTherapistBean = ArrayList<DataBean>()

    val adapter by lazy { ConsumablesDescAdapter(requireContext(), listBean, mCStatus) }

    var mRecyclerView: RecyclerView? = null

    var mCStatus = -1//选中状态

    val consumablesDialog by lazy{ ConsumablesConfigFrg() }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.f_consumables_desc, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mPresenter.init(this)
        this.view = view
        mCStatus = arguments?.getInt("status")!!
        val jsonString = arguments?.getString("list")
        val type = object : TypeToken<ArrayList<DataBean>>() {}.type
        listBean = Gson().fromJson(jsonString, type)

        (activity as MainActivity).setOnActivityResultListener1(this)
        view.findViewById<View>(R.id.bt_confirm).setOnClickListener(this)
        view.findViewById<View>(R.id.back).setOnClickListener { activity!!.onBackPressed() }
        view.findViewById<View>(R.id.menu).setOnClickListener {
            (activity as MainActivity?)!!.updateDrawerStatus()
            (activity as MainActivity?)!!.mDrawerLayout.openDrawer(Gravity.RIGHT)
        }
        setTitle()
        mRecyclerView = view.findViewById(R.id.recyclerView)
        mRecyclerView?.setLayoutManager(LinearLayoutManager(context));
//        mRecyclerView.addItemDecoration(SpaceItemDecoration(DpUtils.dip2px(requireContext(),10f), DpUtils.dip2px(requireContext(),10f)))
        mRecyclerView?.setHasFixedSize(true)
        mRecyclerView?.setNestedScrollingEnabled(true)
        mRecyclerView?.adapter = adapter
        adapter.notifyDataSetChanged()
        mPresenter.onAsk(mCStatus)
    }

    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.bt_confirm -> {
                when(mCStatus) {
                    //领用
                    CONSUMABLES_RECEIVE -> {
                        if (listTeamBean.size == 0){
                            ToastUtil.showShortToast(requireActivity().getString(R.string.text7))
                            return
                        }
                        if (listTherapistBean.size == 0){
                            ToastUtil.showShortToast(requireActivity().getString(R.string.therapist_no_data))
                            return
                        }
                    }
                    //入库
                    CONSUMABLES_WAREHOUSE ->{
                        if (listWarehouseBean.size == 0){
                            ToastUtil.showShortToast(requireActivity().getString(R.string.text8))
                            return
                        }
                    }
                    //补货
                    CONSUMABLES_RESTOCK ->{
                        if (listWarehouseBean.size == 0){
                            ToastUtil.showShortToast(requireActivity().getString(R.string.text8))
                            return
                        }
                        if (listMaterialBean.size == 0){
                            ToastUtil.showShortToast(requireActivity().getString(R.string.text9))
                            return
                        }
                    }
                }
//                val dialog = ConsumablesConfigFrg()
                val args = Bundle()
                args.putInt("mCStatus", mCStatus)
                args.putString("listBean", Gson().toJson(listBean))
                args.putString("listTeamBean", Gson().toJson(listTeamBean))
                args.putString("listWarehouseBean", Gson().toJson(listWarehouseBean))
                args.putString("listMaterialBean", Gson().toJson(listMaterialBean))
                args.putString("listTherapistBean", Gson().toJson(listTherapistBean))
                consumablesDialog.setArguments(args)
                consumablesDialog.show(childFragmentManager, "a")
                consumablesDialog.setOnClickListener(object :ConsumablesConfigFrg.OnClickListener{
                    override fun onSubmitCallback(json: String, listBean: ArrayList<DataBean>) {
                        adapter.notifyDataSetChanged()
                        mPresenter.onUploaded2(requireActivity(), json, listBean)
                    }

                    override fun onWarehouseScanCallback() {
                        mPresenter.toZking(activity)
                    }
                })
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onMessageEvent(event: DialogCallbackEvent){
        if (event.noList.size == 0){
            activity!!.onBackPressed()
        }else{
            listBean.clear()
            listBean.addAll(event.noList)
            adapter.notifyDataSetChanged()
            setTitle()
        }
    }

    fun setTitle(){
        when(mCStatus){
            CONSUMABLES_RECEIVE ->{
                (view.findViewById<View>(R.id.toolbar_title) as TextView).text = getString(R.string.consumables) + " " + getString(R.string.receive1) + "(" + "${listBean.size}" + ")"
            }
            CONSUMABLES_WAREHOUSE ->{
                (view.findViewById<View>(R.id.toolbar_title) as TextView).text = getString(R.string.consumables) + " " + getString(R.string.warehouse) + "(" + "${listBean.size}" + ")"
            }
            CONSUMABLES_RESTOCK ->{
                (view.findViewById<View>(R.id.toolbar_title) as TextView).text = getString(R.string.consumables) + " " + getString(R.string.restock) + "(" + "${listBean.size}" + ")"
            }
        }
    }


    override fun setListCallback(list: java.util.ArrayList<DataBean>) { }
    override fun setMedicalTeamListCallback(list: java.util.ArrayList<DataBean>) {
        listTeamBean.addAll(list)
    }

    override fun setWarehouseListCallback(list: java.util.ArrayList<DataBean>) {
        listWarehouseBean.addAll(list)
    }

    override fun setMaterialListCallback(list: java.util.ArrayList<DataBean>) {
        listMaterialBean.addAll(list)
    }

    override fun setNoData() {
        TODO("Not yet implemented")
    }

    override fun setTherapistsListCallback(list: java.util.ArrayList<DataBean>) {
        listTherapistBean.addAll(list)
    }

    override fun onResult(requestCode: Int, resultCode: Int, data: Intent?) {
        val result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data)
        var contents = result.contents
        if (contents != null){
            consumablesDialog.setScanText(contents)
        }
    }

}